<script setup>
import { ref } from 'vue'
import { memberProfile, memberProfileAvatar } from '@/api/user'
import { useUserStore } from '@/stores/user'
import { ElInput, ElMessage } from 'element-plus'

const store = useUserStore()

// 初始化个人信息
const infoData = ref({
  nickname: '',
  gender: '',
  birthday: '',
  cityCode: '',
  provinceCode: '',
  countryCode: '',
  profession: ''
})
const initInfo = () => {
  infoData.value.nickname = store.userInfo.nickname
  infoData.value.gender = store.userInfo.gender
  infoData.value.birthday = store.userInfo.birthday
  infoData.value.cityCode = store.userInfo.cityCode
  infoData.value.provinceCode = store.userInfo.provinceCode
  infoData.value.countryCode = store.userInfo.countryCode
  infoData.value.profession = store.userInfo.profession
}
initInfo()

const disabledDate = time => {
  return time.getTime() > Date.now()
}

// 修改个人信息
const updateInfo = async () => {
  // console.log(infoData.value)
  await memberProfile(infoData.value)
  ElMessage.success('修改成功')
  store.setUserInfo({
    ...store.userInfo,
    ...infoData.value
  })
}

// 修改头像，这玩意后端阿里云过期了
const fileInput = ref(null)

const handleUploadClick = () => {
  fileInput.value.click()
}

const handleFileChange = async event => {
  const file = event.target.files[0]
  if (file) {
    const formData = new FormData()
    formData.append('file', file)
    const res = await memberProfileAvatar(formData)
    // console.log(res)
  }
}
</script>

<template>
  <div class="user__info">
    <span class="user__info__title">个人信息</span>
    <div class="user__info__avatar">
      <span style="margin-right: 20px">头像：</span>
      <img :src="store.userInfo.avatar" alt="" />
      <el-button style="margin-left: 20px" @click="handleUploadClick">更换头像</el-button>
      <input type="file" ref="fileInput" style="display: none" @change="handleFileChange" />
    </div>
    <div class="user__info__form">
      <div class="user__info__form__row1">
        昵称：<el-input class="input" placeholder="无" v-model="infoData.nickname"></el-input>
      </div>
      <div class="user__info__form__row2">
        性别：
        <el-radio-group v-model="infoData.gender" class="input">
          <el-radio value="男" size="large">男</el-radio>
          <el-radio value="女" size="large">女</el-radio>
          <el-radio value="未知" size="large">未知</el-radio>
        </el-radio-group>
        生日：
        <el-date-picker
          v-model="infoData.birthday"
          type="date"
          placeholder="选择一天"
          :disabled-date="disabledDate"
          size="large"
          value-format="YYYY-MM-DD"
        />
      </div>
      <div class="user__info__form__row3">
        城市编码：
        <el-input class="input" placeholder="请输入城市编码" v-model="infoData.cityCode"></el-input>
      </div>
      <div class="user__info__form__row4">
        省份编码：
        <el-input
          class="input"
          placeholder="请输入省份编码"
          v-model="infoData.provinceCode"
        ></el-input>
      </div>
      <div class="user__info__form__row5">
        区/县编码：
        <el-input
          class="input"
          placeholder="请输入区/县编码"
          v-model="infoData.countryCode"
        ></el-input>
      </div>
      <div class="user__info__profession">
        职业：
        <el-input class="input" placeholder="请输入职业" v-model="infoData.profession"></el-input>
      </div>
      <div class="user__info__form__row6">
        <el-button class="button-save" type="primary" @click="updateInfo">保存</el-button>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.user__info {
  width: 1000px;
  height: 900px;
  padding: 40px;
  background-color: #fff;
  .user__info__title {
    font-size: 30px;
    font-weight: bold;
  }
  .user__info__avatar {
    margin: 40px 0px;
    width: 350px;
    display: flex;
    align-items: center;
    white-space: nowrap;
    img {
      width: 100%;
      border-radius: 100%;
      overflow: hidden;
    }
  }
  .user__info__form {
    width: 100%;
    height: 50px;
    .input {
      width: 300px;
      height: 40px;
      margin: 20px;
    }
  }
  .user__info__profession {
    margin-top: 40px;
    height: 40px;
    display: flex;
    align-items: center;
  }
  .user__info__form__row6 {
    width: 100%;
    text-align: center;
    .button-save {
      margin-top: 40px;
      width: 150px;
      height: 40px;
    }
  }
}
</style>
